package popple.display
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.textures.Texture;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	
	import popple.core.RenderHelper;
	import popple.core.renders.NormalRender;
	import popple.data.StaticData;
	
	/**
	 *最小单元显示对象,可做单元图形显示，也可以单个位图显示
	 * @author Popple
	 * 
	 */	
	public class Image extends DisplayObject
	{
		protected var mDraged:Boolean=false;
		protected var mDragCenter:Boolean=false;
		
		private var _offsetX:Number;
		private var _offsetY:Number;
		protected var mBitmap:TBitmap;
		protected var mTexture:Texture;
		
		
		
		protected var mRender:NormalRender;
		/**
		 *显示图片,Sprite可做容器 
		 * @param bitmap
		 * 
		 */			
		public function Image(bitmap:TBitmap=null)
		{
			super();
			
			mBitmap=bitmap;
			mRender=mRenderHelper
				.getRender(NormalRender) as NormalRender;
			mRender.configureBackBuffer(Stage.width,Stage.height,4,true);
			init();
		}
		public function set bitmap(value:TBitmap):void
		{
			
			mBitmap=value;
		
			init();
		}
		public function dispose():void
		{
			mBitmap.dispose();
			
		}
		private function init():void
		{
			if(mBitmap!=null)
			{
				
				mOrginWidth=mBitmap.width;
				mOrginHeight=mBitmap.height;
				initBuffers();
				initSize();
			}
		}
		//计算初始化大小
		override protected function initBuffers():void
		{
			
			mVertexBuffer=mRenderHelper.getVertexBuffer(StaticData.vertices,3);
			mIndexBuffer=mRenderHelper.getIndexBuffer(StaticData.indexs);
			mTexture=mBitmap.texture;
			mUVBuffer=mBitmap.uv;
		}
		
		/*override public function set width(value:Number):void
		{
			mWidth=value;
			mAScaleX=(mWidth-mOrginWidth)/mOrginWidth+1;
			mAScaleX==0?mAScaleX=1:mAScaleX;
			
		}
		override public function set height(value:Number):void
		{
			mHeight=value;
			mAScaleY=(mHeight-mOrginHeight)/mOrginHeight+1;
			mAScaleY==0?mAScaleY=1:mAScaleY;
			
		}*/
		override protected function draw():void
		{
			
			mRender.render(mVertexBuffer,
				mUVBuffer,
				mIndexBuffer,
				mTexture,
				mColors,
				mCameraM3D,
				mWorldMatrix);
		}
		/*
		public function startDrag(center:Boolean=false):void
		{
			mDragCenter=center;
			mDraged=true;
			
		}
		
		public function stopDrag():void
		{
			mDraged=false;
		}*/
		
	}
}